********************************************************************************
*	PROJECT: Childhood confidence and long-term outcomes (PSID)
*	PURPOSE: Clean CDS files 
*	PUBLISHED: August 2022
*	CONTACT: Hannah Ruebeck, hruebeck@mit.edu
********************************************************************************

clear all
set more off
set maxvar 10000


local path "/Users/XXXXX/Downloads/replication materials"

local RAW "`path'/raw"
local INDIV "`RAW'/Cross_year_indiv"
local CLEAN "`path'/clean"

*******************************************************************************
// MAKE CPI DATASET, TO CONVERT TO 2016 USD

* make cpi dataset from https://www.minneapolisfed.org/about-us/monetary-policy/inflation-calculator/consumer-price-index-1913- 
	* this will put dollars into 1982-1984 dollars. To put into 2016 dollars, divide by the cpi/100 and then multiply by 240.0

	clear
	input pryr cpi
	2004 188.9
	2006 201.6
	2008 215.3
	2010 218.1
	2012 229.6
	2014 236.7
	2016 240.0
	2018 251.1
	end

	tempfile cpi
	save `cpi', replace 
	
********************************************************************************
// BRING IN AND MATCH TAS WAVES

local filelist J269530 J269534 J269535 J269537 J269538 J269539 J269540 J301740
local relationlist ER33803 ER33903 ER34003 ER34103 ER34203 ER34303 ER34503 ER34703
local numlist 5 7 9 11 13 15 17 19
forvalues j = 1/8 {
*local j = 8
	local i: word `j' of `numlist'
	local file: word `j' of `filelist'
	local year = 2000 + `i'
	local headvar: word `j' of `relationlist'
	
	qui do "`RAW'/ta`year'/`file'.do"
	qui do "`RAW'/ta`year'/`file'_formats.do"
	
	*******VARIABLE CLEANING
	gen year = `year'
	gen uid = string(ER30001, "%04.0f") + string(ER30002, "%03.0f")	
	local prefix ta
	
	if `i' < 10 {
	rename TA0`i'0003 int_num_`year'
	rename TA0`i'0004 seq_num_`year'
	}
	else {
	rename TA`i'0003 psid_int_num
	rename TA`i'0004 psid_seq_num
	}
	
	*Variables for relation to reference person (note listed as to head until 2017)
	rename `headvar' relation_to_ref
	
	*****************************
	* year-specific cleaning *
	*****************************
	
	*2005-specific variable cleaning
	if `year' == 2005 {
	drop TA050956 TA050957 TA050958 TA050959 TA050960 TA050961
	*Variables where there are 5 jobs, too similar names
	foreach var of varlist TA050198 TA050237 TA050272 TA050307 TA050342 {
		local label: variable label `var'
		local newlabel = subinstr("`label'","SPEC","",.)
		label variable `var' "`newlabel'"
	}
	}
	*Dropping the following variables because they have repetitive labels 
	*2007-specific variable cleaning
	if `year' == 2007 {
	drop TA070938 TA070939 TA070940 TA070941 TA070942 TA070943 
	}
	*2009-specific variable cleaning
	if `year' == 2009 {
	drop TA091002 TA091003 TA091004 TA091005 TA091006 TA091007 TA091009 TA091010
	}	
	*2011-specific variable cleaning
	if `year' == 2011 {
	drop TA111144 TA111145 TA111146 TA111147 TA111148 TA111149 TA111151 TA111152	
	}	
	*2013-specific variable cleaning
	if `year' == 2013 {
	drop TA131235 TA131236 TA131237 TA131238 TA131239 TA131240 TA131242 TA131243
	}	
	*2015-specific variable cleaning
	if `year' == 2015 {
	drop TA151295 TA151296 TA151297 TA151298 TA151299 TA151300 TA151302 TA151303
	}
	*2017-specific variable cleaning
	if `year' == 2017 {
	drop TA171992 TA171993 TA171994 TA171995 TA171996 TA171997 TA171998 TA171999 TA172000 TA172001 TA172002 TA172003
	drop TA171828-TA171903
	}
	
	*2019-specific variable cleaning
	if `year' == 2019 {
	drop TA190159 TA190158 TA190939 TA190940 TA190950 TA190951 TA190967 TA190968 ///
		TA192103 TA192105 TA192106 TA192111 TA192112 TA192114 TA192115 TA192120 TA192121 TA192123 TA192124
	drop TA191990-TA192065 ER72001-ER77632 ER34701 ER34702
	}
	
	if "`year'" == "2005" local range `"TA050001 TA050002 TA050005-TA050955"'
	if "`year'" == "2007" local range `"TA070001 TA070002 TA070005-TA070937"'
	if "`year'" == "2009" local range `" TA090001 TA090002 TA090005-TA091008 "'
	if "`year'" == "2011" local range `"TA110001 TA110002 TA110005-TA111150"'
	if "`year'" == "2013" local range `"TA130001 TA130002 TA130005-TA131244"'
	if "`year'" == "2015" local range `"TA150001 TA150002 TA150005-TA151304"'
	if "`year'" == "2017" local range `"TA170001 TA170002 TA170005-TA171991"'
	if "`year'" == "2019" local range `"TA190001 TA190002 TA190005-TA192198"'
	
	foreach var of varlist `range' {
	local label: variable label `var'
		    local newvar = lower("`label'")
		    local newvar = subinstr("`newvar'", " 97", "", .)
		    local newvar = subinstr("`newvar'", "-", "", . )
		    local newvar = subinstr("`newvar'", "(", "_", . )
		    local newvar = subinstr("`newvar'", ")", "", . )
		    local newvar = subinstr("`newvar'", "/", "_", . )
		    local newvar = subinstr("`newvar'", "#", "num", . )
		    local newvar = subinstr("`newvar'", "'", "", . )
		    local newvar = subinstr("`newvar'", "&", "and", . )
			local newvar = subinstr("`newvar'", "?", "", . )
		    local newvar = subinstr("`newvar'", ",", "_", . )
		    local newvar = subinstr("`newvar'", "$$", "money", . )
			local newvar = subinstr("`newvar'", "$", "", . )
		    local newvar = subinstr("`newvar'", "<", "under", . )
		    local newvar = subinstr("`newvar'", ">", "over", . )
		    local newvar = subinstr("`newvar'", ".", "", . )
		    local newvar = subinstr("`newvar'", "=", "equal", . )
		    local newvar = subinstr("`newvar'", ":", "_", . )
		    local newvar = subinstr("`newvar'", "%", "pct", . )
		    local newvar = subinstr("`newvar'", "+", "plus", . )
		    local newvar = subinstr("`newvar'", "calss", "class", . )
		    local newvar = subinstr("`newvar'", "first", "1st", . )
		    local newvar = subinstr("`newvar'", "2nd", "second", . )
		    local newvar = subinstr("`newvar'", "3rd", "third", . )
		    local newvar = subinstr("`newvar'", "4th", "fourth", . )
		    local newvar = subinstr("`newvar'", "5th", "fifth", . )
		    local newvar = subinstr("`newvar'", "6th", "sixth", . )
		    local newvar = subinstr("`newvar'", "7th", "seventh", . )
		    local newvar = subinstr("`newvar'", "8th", "eigth", . )
		    local newvar = subinstr("`newvar'", "9th", "ninth", . )
		    local newvar = subinstr("`newvar'", "10th", "tenth", . )
		    local newvar = subinstr("`newvar'", "11th", "eleventh", . )
		    local newvar = subinstr("`newvar'", "12th", "twelfth", . )
		    local newvar = subinstr("`newvar'", "aggravation", "agg", . )
		    local newvar = subinstr("`newvar'", "mention", "men", . )
		    local newvar = subinstr("`newvar'", "differential", "diff", . )
		    local newvar = subinstr("`newvar'", "salary", "sal", . )
		    local newvar = subinstr("`newvar'", "salaried", "sal", . )
		    local newvar = subinstr("`newvar'", "current", "curr", . )
		    local newvar = subinstr("`newvar'", "time unit", "tunit", . )
		    local newvar = subinstr("`newvar'", "ended ", "end", . )
		    local newvar = subinstr("`newvar'", "degree", "deg", . )
		    local newvar = subinstr("`newvar'", "grade point average", "gpa", . )
		    local newvar = subinstr("`newvar'", "possible", "poss", . )
		    local newvar = subinstr("`newvar'", "accuracy of", "accuracy", . )
		    local newvar = subinstr("`newvar'", "before", "b4", . )
		    local newvar = subinstr("`newvar'", "public employ", "public", . )
		    local newvar = subinstr("`newvar'", "private emp", "private", . )
		    local newvar = subinstr("`newvar'","friends_relatives","friend_rel",.)
		    local newvar = subinstr("`newvar'","school_univ","school",.)
		    local newvar = subinstr("`newvar'","jobjob","job",.)
		    local newvar = subinstr("`newvar'","profsnl rgstry","profreg",.)
		    local newvar = subinstr("`newvar'","career or job fair","job fair",.)
		    local newvar = subinstr("`newvar'","funds_royalties","funds_royal",.)
		    local newvar = subinstr("`newvar'","primary","prim",.)
		    local newvar = subinstr("`newvar'","cert_license","certif",.)
		    local newvar = subinstr("`newvar'","certificate_license","certif",.)
		    local newvar = subinstr("`newvar'","how often ","ho_",.)
		    local newvar = subinstr("`newvar'","how much ","hm_",.)
		    local newvar = subinstr("`newvar'","parent num","parent",.)
		    local newvar = subinstr("`newvar'","affection given","affection",.)
		    local newvar = subinstr("`newvar'","num of occasions","times",.)
		    local newvar = subinstr("`newvar'","num of times","times",.)
		    local newvar = subinstr("`newvar'","discrimination","disc",.)
		    local newvar = subinstr("`newvar'", "wtr", "", . )
		    local newvar = subinstr("`newvar'", " ", "", .)
		    if strpos("`newvar'", "time")>0 local newvar = subinstr("`newvar'", "_military", "", .)
		    if strpos("`newvar'", "time")>0 local newvar = subinstr("`newvar'", "_milit", "", .)
		    local newshort = substr("`prefix'_`newvar'", 1, 32)
		    rename `var' `newshort'
		    label variable `newshort' "`var'"
	
	}
	
	***********************************
	* D: Relationship Status *
	***********************************
	
	if `year' == 2017 | `year' == 2019 {
	rename ta_c1currmaritalstatus ta_d1currmaritalstatus
	rename ta_c9cohabiting ta_d4cohabiting
	rename ta_c12romanticrelationshipnow ta_d8romanticrelationshipnow
	rename ta_c45numberofchildren ta_d28anumberofchildren
	rename ta_c46agebecameparent_stepparent ta_d28bageat1stchild
	
	}
	
	***********************************
	* E: Employment Questions *
	***********************************
	
	if `year' == 2017 | `year' == 2019 {
	rename ta_d1employmentstatus1stmen ta_e1employmentstatus1stmen
	rename ta_d2workingformoneynow ta_e3workingformoneynow
	rename ta_d3workedsincejan1ofprioryear ta_e3aworkdsincejan1ofprioryear
	rename ta_d13unempandlookedforworklasty ta_e8aunempandlookedforworklasty
	rename ta_d10outoflaborforcelastyear ta_e7aoutoflaborforcelastyear
		forvalues h = 1/5 {
		replace ta_d27mainind_2012codejob`h' = ta_d27mainind_2012codejob`h'/10
		rename ta_d27mainind_2012codejob`h' ta_e21mainindforjob`h'_2000code 
		}
	}	
	if `year' == 2005 {
	rename ta_e78unempandlookedforworklasty ta_e8aunempandlookedforworklasty
	rename ta_e78outoflaborforcelastyear ta_e7aoutoflaborforcelastyear
	}
	if `year' == 2011 {
		forvalues h = 1/5{
		rename ta_e20mainocc_2010codejob`h' ta_e20mainoccforjob`h'_2000code
		rename ta_e21mainind_2010codejob`h' ta_e21mainindforjob`h'_2000code
		}
	}
	if `year' == 2013 {
		forvalues h = 1/5 {
		rename ta_e20mainocc_2012codejob`h' ta_e20mainoccforjob`h'_2000code
		rename ta_e21mainind_2012codejob`h' ta_e21mainindforjob`h'_2000code 
		}
	}
	if `year' == 2015 {
		forvalues h = 1/5 {
		rename ta_e20mainocc_2000codejob`h' ta_e20mainoccforjob`h'_2000code
		rename ta_e21mainind_2000codejob`h' ta_e21mainindforjob`h'_2000code 
		}
	}
		
	***********************************
	* G: Education questions *
	***********************************
	
	if `year' == 2017 | `year' == 2019 {
	rename ta_g6graduatedhighschool ta_g1graduatedhighschool
	*Using college 1 (most recent), variables for former students
	rename ta_g18pdegreceivedcollegenum1 ta_g13pdegreceivedcollegenum1
	rename ta_g18pdegreceivedcollegenum2 ta_g13pdegreceivedcollegenum2
	rename ta_g18eyearenrolledcollegenum1 ta_g13dyearenrolledcollegenum1 
	rename ta_g18eyearenrolledcollegenum2 ta_g13dyearenrolledcollegenum2	
	}
	if `year' == 2017 | `year' == 2019 {
		rename ta_g18h1stmajoratcollegecollegen ta_g13hmajoratcollegecollegenum1
		rename ta_g18x1stmajorcollegenum1 ta_g13xmajorcollegenum1
		rename ta_g18x1stmajorcollegenum2 ta_g13xmajorcollegenum2
	}
	*Have to do this specially because 8 means something in 2017 but is DK before that

	if inlist(`year',2005,2007,2009) {
	rename ta_g13pdegreceivdmostrecentcolle ta_g13pdegreceivedcollegenum1
	rename ta_g13xmajormostrecentcollege ta_g13xmajorcollegenum1
	rename ta_g13hmajoratmostrecentcollege ta_g13hmajoratcollegecollegenum1
	rename ta_g13pdegreceivdearliercollege ta_g13pdegreceivedcollegenum2
	rename ta_g13xmajorearliercollege ta_g13xmajorcollegenum2
	rename ta_g13dyearenrolledmostrecentcol ta_g13dyearenrolledcollegenum1
	rename ta_g13dyearenrolledinearliercoll ta_g13dyearenrolledcollegenum2
	}
	
	***********************************
	* H: Health questions *
	***********************************
	
	if `year' == 2017 | `year' == 2019 {
	rename ta_h83aho_feltnervousinpastmo ta_h14aho_feltnervousinpastmo
	rename ta_h83bho_felthopelessinpastmo ta_h14bho_felthopelessinpastmo
	rename ta_h83cho_feltrestlessinpastmo ta_h14cho_feltrestlessinpastmo
	rename ta_h83dho_felteverythingeffort ta_h14dho_felteverythingeffort
	rename ta_h83eho_felttoosadinpastmo ta_h14eho_felttoosadinpastmo
	rename ta_h83fho_feltworthlesspastmo ta_h14fho_feltworthlessinpastmo
	rename ta_h83ghm_interferesw_activities ta_h14jhm_interfereswithactiviti
	rename ta_h103drinkalcohol ta_h36drinkalcoholhead
	rename ta_h104ho_havedrinks ta_h37ho_havedrinkshd
	rename ta_h106numdayshad45drinks ta_h39numdayshad45drinkshead
	rename ta_h86 ta_h29smokecigarettes
	rename ta_h87numcigarettesperday ta_h30numcigarettesperday
	}
	
	***********************************
	* K: Risky behavior *
	***********************************
	
	if `year' == 2017 | `year' == 2019 {
	rename ta_k9aho_didsomethingdangerous ta_k11ho_didsomethingdangerous
	rename ta_k9bho_damagedpublicproperty ta_k12ho_damagedpublicproperty
	rename ta_k9cho_gotintophysicalfight ta_k13ho_gotintophysicalfight
	rename ta_k9dho_drovewhendrunkorhigh ta_k15ho_drovewhendrunkorhigh
	rename ta_k9eho_rodewithdrunkdriver ta_k16ho_rodewithdrunkdriver
	}
	
	if `year' == 2017 {
	rename ta_k10arrestedonceormore ta_k17arrestedonceormore
	rename ta_k16onprobationonceormore ta_k18onprobationonceormore
	rename ta_k22injailonceormore ta_k19injailonceormore
	}
	
	if `year' == 2019 {
		foreach var in ta_k10aeverarrested ta_k16aeveronprobation ta_k22aeverservedjail {
			replace `var' = 2 if `var' == 1
			replace `var' = 1 if `var' == 5
		}
		rename ta_k10aeverarrested ta_k17arrestedonceormore
		rename ta_k16aeveronprobation ta_k18onprobationonceormore
		rename ta_k22aeverservedjail ta_k19injailonceormore
	}
	
	
	***********************************
	* M: Variables about how you feel *
	***********************************

	if `year' == 2017 | `year' == 2019 {
	rename ta_b18afreqofhappinessinlastmont ta_m1frequencyofhappinessinlastm
	rename ta_b18bfreqinterestinlifeinlastm ta_m2freqofinterestinlifeinlastm
	rename ta_b18cfreqfeelingsatisfiedinlas ta_m3freqoffeelingsatisfiedinlas
	rename ta_b18dfreqoffeelingcontribtosoc ta_m4freqoffeelingcontribtosocie
	rename ta_b18efreqoffeelingbelongtocomm ta_m5freqoffeelingbelongingtocom
	rename ta_b18ffreqfeelingsocietygettngb ta_m6freqoffeelingsocietygettngb
	rename ta_b18gfreqfeelingpeoplebasicall ta_m7freqoffeelingpeoplebasicall
	rename ta_b18hfreqfeelwaysocworkmakesse ta_m8freqfeelngwaysocworksmakess
	rename ta_b18jfreqfeelmanagedailyresons ta_m9freqfeelmanagngdailyrespons
	rename ta_b18kfreqfeeltrustingrelsw_oth ta_m10freqfeelingtrustingrelsw_o
	rename ta_b18lfreqoffeelingchallengedto ta_m11freqoffeelingchallengedtog
	rename ta_b18mfreqfeelingconfidentownid ta_m12freqfeelingconfidentofowni
	rename ta_b18nfreqoffeelinglikedpersona ta_m13freqoffeelinglikedpersonal
	rename ta_b18ofreqoffeelinglifehaddirec ta_m14freqoffeelinglifehaddirect
	}
	*These are coded differently in 2019 than in 2013/2015, but it's okay because we standardize
	*later by year
	if `year' == 2019 {
		rename ta_b19athoroughworker ta_m15freqsomeonewhodoesthorough 
		rename ta_b19bcommunicative_talkative ta_m16freqsomeonewhoistalkative 
		rename ta_b19csometimesrudetoothers ta_m17freqsomeonewhoisrude 
		rename ta_b19doriginal_comeupw_newideas ta_m18freqsomeonewhoisoriginal 
		rename ta_b19eworrier ta_m19freqsomeonewhoworriesalot 
		rename ta_b19fforgiving ta_m20freqsomeonewhohasforgivngn 
		rename ta_b19gsomewhatlazy ta_m21freqsomeonewhoislazy 
		rename ta_b19houtgoing_sociable ta_m22freqsomeonewhoisoutgoing_s 
		rename ta_b19jvalueartistic_aestheticex ta_m23freqsomeonewhovaluesart 
		rename ta_b19knervous ta_m24freqsomeonewhogetsnervouse 
		rename ta_b19leffective_efficientcomple ta_m25freqsomeonewhodoesthngseff 
		rename ta_b19mreserved ta_m26freqsomeonewhoisreserved 
		rename ta_b19nconsiderate_kindtoothers ta_m27freqsomeonewhoisconsiderat 
		rename ta_b19oimaginative ta_m28freqsomeonewhohasactivimag 
		rename ta_b19prelaxed_abletodealw_stres ta_m29freqsomeonewhoisrelaxed
	}
	
	***********************************
	* Pre-constructed indices *
	***********************************
	
	if `year' == 2017 | `year' == 2019 {
	rename ta_mentalhealth_social ta_2017mentalhealth_social
	}
	else {
	rename ta_mentalhealth_social ta_pre2017mentalhealth_social
	}
	
	***********************************
	* Fixing interview dates *
	***********************************
	rename ta_monthof`year'psidinterview ta_month_psidinterview
	rename ta_yearof`year'psidinterview ta_year_psidinterview
	rename ta_dayof`year'psidinterview ta_day_psidinterview
	if `i' < 15 {
	rename ta_monthof`year'tainterview ta_month_tainterview
	rename ta_yearof`year'tainterview ta_year_tainterview
	rename ta_dayof`year'tainterview ta_day_tainterview
	rename ta_`year'tainterview_idnumber ta_idnumber
	}
	else {
	rename ta_monthof`year'tasinterview ta_month_tainterview
	rename ta_yearof`year'tasinterview ta_year_tainterview
	rename ta_dayof`year'tasinterview ta_day_tainterview
	rename ta_`year'tasinterview_idnumber ta_idnumber
	}
	
	*******Now making locals with each variable label to append into a list
	foreach var of varlist _all {
	local stem= subinstr("`var'","ta_","",1)
	local `stem'`i': variable label `var'
	}

	tempfile tas`i'
	save `tas`i'', replace	
	}
		
   
********************************************************************************
// APPEND AND SAVE TAS WAVES

use `tas5', clear
	tempfile master_tas 
	save `master_tas', replace
	
forvalues i = 7(2)19 {
	use `tas`i'', clear
	append using `master_tas'
	save `master_tas', replace
}

foreach var of varlist _all {
local stem= subinstr("`var'","ta_","",1)
local newlabel = strtrim("``stem'5' ``stem'7' ``stem'9' ``stem'11' ``stem'13' ``stem'15' ``stem'17' ``stem'19' ")
label variable `var' "`newlabel'"
}

*Dropping the extra year-specific interview and sequence numbers, since we also get them from 
*the master 1968 files
drop ER34301 ER34302 ER34201 ER34202 ER34101 ER34102 ER34001 ER34002 ER33901 ER33902 ER33801 ER33802 ER34501 ER34502
	
********************************************************************************
// NOW CLEAN THE COMBINED DATA

replace ta_currstate = . if ta_currstate == 99

	***********************************
	* Introductory variables *
	***********************************
	
	local intro_vars_tokeep uid year ER30001 ER30002 ta_interviewmo ta_day_tainterview ta_idnumber ta_month_psidinterview ta_year_psidinterview ta_day_psidinterview
	
	*Clean the TAS interview date
	gen ta_interviewmo = ym(ta_year_tainterview,ta_month_tainterview)
		lab var ta_interview "Month-year of TAS interview"
	drop ta_year_tainterview ta_month_tainterview
	
	***********************************
	* A: Extracurriculars, etc. *
	***********************************
	
	local a_vars_tokeep
	
	***********************************
	* B: Responsibility variables *
	***********************************
	
	local b_vars_tokeep ta_b6bhowgoodatproblemsolving 
	
	foreach var in ta_b6bhowgoodatproblemsolving {
		replace `var' = . if inlist(`var',8,9)
	}
	
	***********************************
	* C: "How good at..." questions *
	***********************************
	
	local c_vars_tokeep ta_c1* ta_c2*feel* ta_c2*nervous* ta_c2*worry* ta_b26* ta_b27*
	
	*C2 questions (how often nervous meeting others, feel shy, feel self-conscious, etc.) change their scale going into the 2017 round
	foreach var in ta_c1ahowgoodatsupervisingcomp ta_c1bhowgoodatleadingcompw_otrs ta_c1chowgoodatlogiccompw_otrs ta_c1dhowgoodathelpingcompw_otrs ta_c1ehowintelligentcomparedw_ot ta_c1fhowindependentcomparedw_ot ta_c1ghowconfidentcomparedw_othe ta_c1hhowdecisivecomparedw_other ta_c1jhowwelllistencomparedw_oth ta_c1khowgoodatteachingcomparedw ta_c2aho_nervousmeetngothers ta_c2bho_feelshy ta_c2cho_feelselfconscious ta_c2dho_worryaboutmoney ta_c2eho_worryabtfuturejob ta_c2fho_feeldiscouraged ta_c2gho_feelnervousperforming ta_b26aho_nervousmeetingothers ta_b26bho_feelshy ta_b26cho_feelselfconscious ta_b26dho_feelnervousperforming ta_b27afeellikepersonofworth ta_b27bfeelhasnumberofgoodqualit ta_b27cinclinedtofeellikefailure ta_b27dabletodothingsaswellasoth ta_b27efeelnotmuchtobeproudof ta_b27ftakepositiveattitudetowar ta_b27gsatisfiedwithself ta_b27hwishhadmorerespectforself ta_b27jfeeluselessattimes ta_b27kthinkselfnogoodatall {
	replace `var' = . if inlist(`var',8,9)
	}
	
	***********************************
	* D: Marriage and kids questions *
	***********************************
	
	local d_vars_tokeep ta_d8romanticrelationshipnow ta_d4cohabiting ta_d1currmaritalstatus ta_d11howsatisfiedw_relationship ta_c13howsatisfied ta_d28bageat1stchild ta_d28anumberofchildren
	
	*Fixing missing patterns
	foreach var in ta_d1currmaritalstatus {
	replace `var' = . if inlist(`var',8,9)
	}
	
	foreach var in ta_d4cohabiting ta_d8romanticrelationshipnow ta_d11howsatisfiedw_relationship ta_c13howsatisfiedw_relationship {
	replace `var' = . if inlist(`var',8,9,0)
	}
	
	foreach var in ta_d28anumberofchildren {
	replace `var' = . if inlist(`var',98,99)
	}
	
	foreach var in ta_d28bageat1stchild  {
	replace `var' = . if inlist(`var',98,99,0)
	}
	
				
	*Fixing indicators
	label define no_val 0 "No" 1 "Yes"
	foreach var in ta_d4cohabiting ta_d8romanticrelationshipnow {
	replace `var' = 0 if `var' == 5
	label values `var' no_val
	}	
	 
	***********************************
	* E: Employment variables *
	***********************************
	
	local e_vars_tokeep ta_e7aoutoflaborforcelastyear ta_e1employmentstatus1stmen ta_e3workingformoneynow ta_e3aworkdsincejan1ofprioryear ta_e8aunempandlookedforworklasty ta_earningsfromworklastyear ta_d26mainocc_2010codejob1 ta_d26mainocc_2010codejob2 ta_e21mainindforjob1_2000code ta_e21mainindforjob2_2000code ta_e20mainoccforjob1_2000code ta_e20mainoccforjob2_2000code

	foreach var in ta_e8aunempandlookedforworklasty ta_e7aoutoflaborforcelastyear ta_e3workingformoneynow ta_e3aworkdsincejan1ofprioryear {
	replace `var' = . if inlist(`var',8,9)
	}
		
	foreach var in ta_earningsfromworklastyear {
	replace `var' = . if inlist(`var',9999999)
	}
	foreach var in ta_d26mainocc_2010codejob1 ta_d26mainocc_2010codejob2 {
		replace `var' = . if inlist(`var',9999,0)
	}
	foreach var in ta_e20mainoccforjob1_2000code ta_e20mainoccforjob2_2000code {
		replace `var' = . if inlist(`var',999,0)
	}
	foreach var in ta_e21mainindforjob1_2000code ta_e21mainindforjob2_2000code {
		replace `var' = . if inlist(`var',998,999,0)
		replace `var' = . if `var' > 999.8 & `var' < 1000
	}
	foreach var in ta_e1employmentstatus1stmen {
		replace `var' = . if inlist(`var',98,99)
	}
	
	*These variables equal 0 if they already said somewhere that they're working for money
	replace ta_e3workingformoneynow = 1 if ta_e3workingformoneynow == 0
	replace ta_e3aworkdsincejan1ofprioryear = 1 if ta_e3aworkdsincejan1ofprioryear == 0
	**Fixing indicators
	foreach var in ta_e3workingformoneynow ta_e3aworkdsincejan1ofprioryear ta_e8a ta_e7a {
	replace `var' = 0 if `var' == 5
	label values `var' no_val
	}
	
	***********************************
	* E (cont.): Military questions *
	***********************************
	
	local e2_vars_tokeep 
	
	***********************************
	* F: Income questions *
	***********************************
	
	local f_vars_tokeep ta_f1hm ta_e2aamtofwages_sal ta_e3aamountofbonusincome ta_e3bamountofotincome ///
	ta_e3camountoftipsincome ta_e3damountofcommissionsincome ta_e3eamountofotherlaborincome
	
	*Keeping zero because that means they didn't get any, though technically also includes 
	*if they don't know whether thye got it or not
	foreach var in ta_e3aamountofbonusincome ta_e3bamountofotincome ta_e3camountoftipsincome ta_e3damountofcommissionsincome ta_e3eamountofotherlaborincome {
	replace `var' = . if inlist(`var',999998,999999)
	}
	
	foreach var in ta_f1hm ta_e2aamtofwages_sal {
	replace `var' = . if inlist(`var',9999998,9999999)
	}
	
	***********************************
	* G: Education questions *
	***********************************
	
	local g_vars_tokeep ta_enrollmentstatus ta_g1graduatedhighschool ta_g13pdegreceivedcollegenum1 ta_g13pdegreceivedcollegenum2 ta_g13xmajorcollegenum1 ta_g13xmajorcollegenum2 ta_g13hmajoratcollegecollegenum1 ///
	ta_g13dyearenrolledcollegenum1 ta_g13dyearenrolledcollegenum2 ta_g36howsuccessfulinjobmostlike ta_g37likelihoodofhavingjobmostl ta_g40ahowgoodinjobrequiringmath ta_g40bhowgoodinjobrequiringtech ta_g40chowgoodinjobw_lotofread_w
	
	foreach var in ta_g1graduatedhighschool ta_g36howsuccessfulinjobmostlike ta_g37likelihoodofhavingjobmostl ///
	ta_g40ahowgoodinjobrequiringmath ta_g40bhowgoodinjobrequiringtech ta_g40chowgoodinjobw_lotofread_w {
	replace `var' = . if inlist(`var',8,9,0)
	}
	foreach var in ta_g13xmajorcollegenum1 ta_g13xmajorcollegenum2 ta_g13hmajoratcollegecollegenum1 {
	replace `var' = . if inlist(`var',998,999,0) & year != 2019
	replace `var' = . if inlist(`var',99,0) & year == 2019
	}
	foreach var in ta_g13pdegreceivedcollegenum1 ta_g13pdegreceivedcollegenum2 {
	replace `var'= . if inlist(`var',8,9,0,98,99,97)
	}
	replace ta_enrollmentstatus = . if inlist(ta_enrollment,95,96,98,99)
	
	foreach var in ta_g13dyearenrolledcollegenum1 ta_g13dyearenrolledcollegenum2 {
	replace `var' = . if inlist(`var',9996,9998,9999,0)
	}

	***********************************
	* H: Health questions *
	***********************************	
	
	local h_vars_tokeep ta_h14aho_feltnervousinpastmo ta_h14bho_felthopelessinpastmo ///
	ta_h14cho_feltrestlessinpastmo ta_h14dho_felteverythingeffort ta_h14eho_felttoosadinpastmo /// 
	ta_h14fho_feltworthlessinpastmo ta_h29smokecig ta_h30numcigarettesperday ta_h36drinkalcoholhead ///
	ta_h37ho_havedrinkshd ta_h39numdayshad45drinkshead ta_h12everhademotion
	
	foreach var in ta_h12everhademotion ta_h14aho_feltnervousinpastmo ta_h14bho_felthopelessinpastmo ta_h14cho_feltrestlessinpastmo ta_h14dho_felteverythingeffort ta_h14eho_felttoosadinpastmo ta_h14fho_feltworthlessinpastmo ta_h36drinkalcoholhead ta_h29smokecig {
	replace `var' = . if inlist(`var',8,9)
	}
	
	*The drinking and smoking variables 
	foreach var in ta_h37ho_havedrinkshd {
		replace `var' = . if inlist(`var',8,9,0)
	}
	foreach var in ta_h39numdayshad45drinkshead ta_h30numcigarettesperday {
		replace `var' = . if inlist(`var',998,999,0)
	}
		
	*Fix indicators
	foreach var of varlist ta_h36drinkalcoholhead ta_h29smokecig ta_h12everhademotion {
	replace `var' = 0 if `var' == 5
	label values `var' no_val
	}
	*Setting some variables to 0 if they never drink or smoke
	replace ta_h37ho_havedrinkshd = 0 if ta_h36drinkalcoholhead == 0
	replace ta_h39numdayshad45drinkshead = 0 if ta_h36drinkalcoholhead == 0
	replace ta_h30numcigarettesperday = 0 if ta_h29smokecig == 0
		foreach var in ta_h37ho_havedrinkshd ta_h39numdayshad45drinkshead ta_h30numcigarettesperday {
			label values `var' .
		}
		
		
	
	***********************************
	* K: Risky behaviors *
	***********************************
	
	local k_vars_tokeep ta_k11ho_didsomethingdangerous ta_k12ho_damagedpublicproperty ta_k13ho_gotintophysicalfight /// 
	ta_k15ho_drovewhendrunkorhigh ta_k16ho_rodewithdrunkdriver ta_k17arrestedonceormore ///
	ta_k18onprobationonceormore ta_k19injailonceormore
		
	foreach var in ta_k11ho_didsomethingdangerous ta_k12ho_damagedpublicproperty ta_k13ho_gotintophysicalfight ta_k15ho_drovewhendrunkorhigh ta_k16ho_rodewithdrunkdriver ta_k17arrestedonceormore  {
	replace `var' = . if inlist(`var',8,9)
	}
	
	foreach var in ta_k18onprobationonceormore ta_k19injailonceormore {
	replace `var' = . if inlist(`var',8,9,0)	
	}
	*Say never on probation /in jail if never arrested, instead of missing
	replace ta_k18onprobationonceormore = 1 if ta_k17arrestedonceormore == 1
	replace ta_k19injailonceormore = 1 if ta_k17arrestedonceormore == 1
	
	***********************************
	* L: Religious beliefs and race *
	***********************************
	
	local l_vars_tokeep 
	
	***********************************
	* M: Variables about how you feel *
	***********************************
	
	local m_vars_tokeep ta_m*freq*
	
	drop ta_m5freqoffeelingbelongingtocom
foreach var in ta_m1frequencyofhappinessinlastm ta_m2freqofinterestinlifeinlastm ta_m3freqoffeelingsatisfiedinlas ta_m4freqoffeelingcontribtosocie ta_m6freqoffeelingsocietygettngb ta_m7freqoffeelingpeoplebasicall ta_m8freqfeelngwaysocworksmakess ta_m9freqfeelmanagngdailyrespons ta_m10freqfeelingtrustingrelsw_o ta_m11freqoffeelingchallengedtog ta_m12freqfeelingconfidentofowni ta_m13freqoffeelinglikedpersonal ta_m14freqoffeelinglifehaddirect ta_m15freqsomeonewhodoesthorough ta_m16freqsomeonewhoistalkative ta_m17freqsomeonewhoisrude ta_m18freqsomeonewhoisoriginal ta_m19freqsomeonewhoworriesalot ta_m20freqsomeonewhohasforgivngn ta_m21freqsomeonewhoislazy ta_m22freqsomeonewhoisoutgoing_s ta_m23freqsomeonewhovaluesart ta_m24freqsomeonewhogetsnervouse ta_m25freqsomeonewhodoesthngseff ta_m26freqsomeonewhoisreserved ta_m27freqsomeonewhoisconsiderat ta_m28freqsomeonewhohasactivimag ta_m29freqsomeonewhoisrelaxed {
	replace `var' = . if inlist(`var',8,9)
	} 

	***********************************
	* W: Variables about profit, loans *
	***********************************
	
	local w_vars_tokeep
	
	***********************************
	* 2017 pre-constructed indices *
	***********************************
	
	local other_vars_tokeep ta_marital_cohab ta_responsibilities_financial ta_mentalhealth_worry ta_2017mentalhealth_social  ta_pre2017mentalhealth ta_subscale_emotionalwb ta_subscale_socialwb ta_subscale_psychol ta_mentalhealth_riskybehaviors ta_mentalhealth_languish  ta_mentalhealth_nonspecpsychdist ta_urbanicity ta_highesteducation 
	
	foreach var in ta_marital_cohab {
	replace `var' = . if inlist(`var',9,98,99)
	}	
	foreach var in ta_responsibilities_financial ta_mentalhealth_worry ta_2017mentalhealth_social  ta_pre2017mentalhealth ta_subscale_emotionalwb ta_subscale_socialwb ta_subscale_psychol ta_mentalhealth_riskybehaviors {
	replace `var' = . if `var' == 9
	}	
	foreach var in  ta_mentalhealth_languish  ta_mentalhealth_nonspecpsychdist {
	replace `var' = . if `var' == 99
	}	
	foreach var in ta_urbanicity {
	replace `var' = . if inlist(`var',99,0)
	}
	foreach var in ta_highesteducation {
	replace `var' = . if inlist(`var',96,99)
	}

	
********************************************************************************
// KEEP THE VARIABLES IN OUR LISTS

	keep `intro_vars_tokeep' `a_vars_tokeep' `b_vars_tokeep' `c_vars_tokeep' `d_vars_tokeep' `e_vars_tokeep' 	`e2_vars_tokeep' `f_vars_tokeep' `g_vars_tokeep' `h_vars_tokeep' `k_vars_tokeep' `l_vars_tokeep' `m_vars_tokeep' `w_vars_tokeep'
	
********************************************************************************
// NOW DO SECOND-LEVEL CLEANING

	**first merge in age and weights from CDS files. 
	merge m:1 uid using "`CLEAN'/agefile.dta"
		drop if _merge == 2
		drop _merge
		
	merge m:1 uid using "`CLEAN'/weightfile.dta"
		drop if _merge == 2
		drop _merge
		
	gen age = year - birthyear
		gen overage22 = (age >= 23 & !mi(age))
			replace overage22 = . if mi(age)
			label values overage22 no_val
			lab var overage22 "Is over the age of 22"
	
	***********************************
	* Education outcomes *
	***********************************
	
	*CATEGORIZING ENROLLMENT VARIABLES
	*Just going to use the variables for completed degrees and enrolled grad
	sort ER30001 ER30002 year 
	gen ta_enrollment_full = ta_enrollmentstatus
	bys ER30001 ER30002: replace ta_enrollment_full = ta_enrollmentstatus[_n-1] if mi(ta_enrollmentstatus[_n]) & !mi(ta_enrollmentstatus[_n-1])
	label values ta_enrollment_full TA171980L 
	drop ta_enrollmentstatus 
	
	gen mi_ta_enroll = (!mi(ta_enrollment_full))
	bys ER30001 ER30002: egen num_nm_enroll = total(mi_ta_enroll) 
	foreach i in 1 2 3 4 5 6 7 9 10 11 {
		gen enroll`i' = (ta_enrollment_full == `i')
		bys ER30001 ER30002: egen has_enroll`i' = total(enroll`i')
	}
	
	*WTR GRADUATED HIGH SCHOOL
	gen grad_hs = (ta_g1graduatedhighschool == 1)
		replace grad_hs = . if mi(ta_g1graduatedhighschool)
	bys ER30001 ER30002: egen atleast_grad_hs = max(grad_hs)
		*Filling in as yes if enrollment var says has HS dip
	replace atleast_grad_hs = 1 if has_enroll3 >= 1
	drop grad_hs 
	label values atleast_grad_hs no_val
	*gen alt_max_grad_hs = (has_enroll3 >= 1 | has_enroll4 >= 1 | has_enroll5 >= 1 | has_enroll6 >= 1 | has_enroll7 >= 1 | has_enroll11 >= 1)
	*Not doing this alternative measure for having HS diploma because you can do later schooling with a GED
		
	*WTR GRADUATED HIGH SCHOOL OR GOT GED
	gen grad_hs_ged = (inlist(ta_g1graduatedhighschool,1,2))
		replace grad_hs_ged = . if mi(ta_g1graduatedhighschool)
	bys ER30001 ER30002: egen atleast_grad_hs_ged = max(grad_hs_ged)
		drop grad_hs_ged
		*Filling in as yes if enrollment var says has HS dip or GED, or was enrolled in post-HS education
	replace atleast_grad_hs_ged = 1 if (has_enroll2 >=1 | has_enroll3 >= 1 | has_enroll4 >= 1 | has_enroll5 >= 1 | has_enroll6 >= 1 | has_enroll7 >= 1 | has_enroll10 >= 1 | has_enroll11 >= 1)
	*Setting to 0 if there is no year where there is evidence of a previous degree 
	replace atleast_grad_hs_ged = 0 if (has_enroll2 ==0 & has_enroll3 ==0 & has_enroll4 ==0 & has_enroll5 ==0 & has_enroll6 ==0 & has_enroll7 ==0 & has_enroll10 ==0 & has_enroll11 ==0) & mi(atleast_grad_hs_ged) & num_nm_enroll != 0
		label values atleast_grad_hs_ged no_val

	*WTR GRADUATED COLLEGE
	*A little complicated because there is the G13P variable about most recent degree received, but sometimes that's missing even if enrollment says that they graduated from college.
	gen grad_bach = (ta_g13pdegreceivedcollegenum1 == 2 | ta_g13pdegreceivedcollegenum2 == 2)
		replace grad_bach = . if mi(ta_g13pdegreceivedcollegenum1) & mi(ta_g13pdegreceivedcollegenum2)
	bys ER30001 ER30002: egen atleast_grad_bach = max(grad_bach)
		drop grad_bach
	replace atleast_grad_bach = 1 if has_enroll6 >= 1
	*Setting to 0 if there's no evidence that they graduated college and currently missing
	replace atleast_grad_bach = 0 if has_enroll6 == 0 & mi(atleast_grad_bach) & num_nm_enroll != 0
		label values atleast_grad_bach no_val
		
	*GRAD SCHOOL
	gen atleast_grad_postbach = (has_enroll7 >=1 & atleast_grad_bach == 1)
		replace atleast_grad_postbach = . if num_nm_enroll == 0
		label values atleast_grad_postbach no_val
		
	drop has_enroll* enroll* *mi_ta_enroll  num_nm_enroll
	
	***********************************
	* College major variables *
	***********************************
	
	local major_vars ta_g13xmajorcollegenum1 ta_g13xmajorcollegenum2 ta_g13hmajoratcollegecollegenum1 
	local new_major_vars ta_g13x_coll1_majorcat ta_g13x_coll2_majorcat ta_g13h_coll_majorcat
	
	forvalues i = 1/3 {
		local major_var: word `i' of `major_vars'
		local new_var: word `i' of `new_major_vars'
		
		*First we need to categorize college major into chunks
		gen `new_var' = ""
		replace `new_var' = "Business-related fields" if inlist(`major_var',15, 16,18,20,92,93,94,109,1,2,121) & year != 2019
		replace `new_var' = "Engineering" if inlist(`major_var',21,22,23,24,25,26,27,28,29,30,31,32,33,120) & year != 2019
		replace `new_var' = "Math and computer science" if inlist(`major_var',34,35,36,37,113,122,87) & year != 2019
		replace `new_var' = "Natural sciences" if inlist(`major_var',38,39,40,41,42,43,44,45,47,48,49) & year != 2019
		replace `new_var' = "Health fields" if inlist(`major_var',46,50,51,52,53,54,55,56,57,88,90,91,116,119,123) & year != 2019
		replace `new_var' = "Teaching" if inlist(`major_var',58,59,60) & year != 2019
		replace `new_var' = "Social science" if inlist(`major_var',61,62,63,64,65,67,68,69,107,108,110,112,114,115,118,70,71,72,17,19) & year != 2019
		replace `new_var' = "Writing and arts" if inlist(`major_var',73,74,75,76,77,78,79,80,81,82,83,84,85,89,117,186,198) & year != 2019
		replace `new_var' = "Services" if inlist(`major_var', 86,98,99,100,101,102,103,104,105,3,6,7,8,9,95,96,97) & year != 2019
		replace `new_var' = "Other" if mi(`new_var') & year != 2019 & !mi(`major_var')
		
		*Now doing the categorizations for 2019 majors
		replace `new_var' = "Business-related fields" if inlist(`major_var',52) & year == 2019
		replace `new_var' = "Engineering" if inlist(`major_var',1,4,14,15,29) & year == 2019
		replace `new_var' = "Math and computer science" if inlist(`major_var',11,27) & year == 2019
		replace `new_var' = "Natural sciences" if inlist(`major_var',3,26,40,41) & year == 2019
		replace `new_var' = "Health fields" if inlist(`major_var',34,51) & year == 2019
		replace `new_var' = "Teaching" if inlist(`major_var',13) & year == 2019
		replace `new_var' = "Social science" if inlist(`major_var',5,19,22,38,39,42,45,54) & year == 2019
		replace `new_var' = "Writing and arts" if inlist(`major_var',9,16,23,25,50) & year == 2019
		replace `new_var' = "Services" if inlist(`major_var', 10,12,31,36,43,44,46,47,48,49) & year == 2019
		replace `new_var' = "Other" if mi(`new_var') & year == 2019 & !mi(`major_var')
		
		encode `new_var', gen(`new_var'2)
			drop `new_var' 
			*`major_var'
			rename `new_var'2 `new_var'
	}
	
	gen bach_major1 = (!mi(ta_g13x_coll1_majorcat) & ta_g13pdegreceivedcollegenum1 == 2)
	gen bach_major2 = (!mi(ta_g13x_coll2_majorcat) & ta_g13pdegreceivedcollegenum2 == 2)
	*Going to keep the last major listed
	tempfile full_data
		save `full_data', replace
	
	keep if bach_major1 == 1 | bach_major2 == 1	
	gen bach_major1years = year if bach_major1 == 1
	gen bach_major2years = year if bach_major2 == 1
	bys uid: egen last_year_major1 = max(bach_major1years)
		replace last_year_major1 = 0 if mi(last_year_major1)
	bys uid: egen last_year_major2 = max(bach_major2years)
		replace last_year_major2 = 0 if mi(last_year_major2)
	gen ta_major_cat = ta_g13x_coll1_majorcat if last_year_major1 >= last_year_major2 & year == last_year_major1
	replace ta_major_cat = ta_g13x_coll2_majorcat if last_year_major2 > last_year_major1 & year == last_year_major2
	keep uid ta_major_cat
	keep if !mi(ta_major_cat)	
	merge 1:m uid using `full_data'
		drop _merge bach_major1 bach_major2 
	save `full_data', replace

	*Now replacing with current major if don't have major_touse and atleast_grad_bach=1, and if status is currently enrolled w/ no prior degree
	*So these are people that are maybe missing designations that they graduated later, but then went to grad school, etc. 
	keep if atleast_grad_bach == 1 & mi(ta_major_cat) & !mi(ta_g13h_coll_majorcat)
	*Keeping the latest year for which major is available
	bys uid: egen last_year_currmajor = max(year)
	keep if year == last_year_currmajor
	keep uid ta_g13h_coll_majorcat
	rename ta_g13h_coll_majorcat ta_major_cat
	gen ta_use_curr_major = 1
	merge 1:m uid using `full_data'
		drop _merge
		replace ta_use_curr_major = 0 if mi(ta_use_curr_major) & !mi(ta_major_cat)
	
	*Dropping old major variables
	drop ta_g13*major*
		
	***********************************
	* Diagnosed psych problems/health *
	***********************************
	
	bys uid: egen ta_everhadpsychdiag = max(ta_h12everhademotion)
		lab values ta_everhadpsychdiag no_val
		lab var ta_everhadpsychdiag "Ever had diagnosis of psych problem"
		
	drop ta_h12* 
	
	***********************************
	* Risky behaviors *
	***********************************
	
	*ALCOHOL CONSUMPTION
	gen drink_alc_often = (inlist(ta_h37ho_havedrinkshd,5,6))
		replace drink_alc_often = . if mi(ta_h37ho_havedrinkshd)
		lab var drink_alc_often "Drink alcohol at least several times a week"
		
	gen binge_drink_alc = (ta_h39num >= 12 & !mi(ta_h39num))
		replace binge_drink_alc = . if mi(ta_h39num)
		lab var binge_drink_alc "Have >=4-5 drinks on at least 12 days"
		
	drop ta_h36drinkalcoholhead ta_h37ho_havedrinkshd ta_h39numdayshad45drinkshead
		
	*SMOKING CIGARETTES
	gen smoke_cig_regularly = (ta_h29smokecig == 1 & ta_h30numcigarettesperday >= 2)
		replace smoke_cig_reg = . if mi(ta_h30numcigarettesperday)
		lab var smoke_cig_reg "Have at least 2 cigarettes per day"
	drop ta_h29smoke ta_h30num 
		
	*OTHER DANGEROUS BEHAVIORS
	gen ta_everarrested = (inlist(ta_k17arrestedonceormore,2,3))
		replace ta_everarrest = . if mi(ta_k17arrestedonceormore)
		lab var ta_everarrested "Ever arrested"
	gen ta_everprobation = (inlist(ta_k18onprobationonceormore,2,3))
		replace ta_everprobation = . if mi(ta_k18onprobationonceormore)
		lab var ta_everprobation "Ever on probation after arrest"
	gen ta_everjailed = (inlist(ta_k19injailonceormore,2,3))
		replace ta_everjailed = . if mi(ta_k19injailonceormore)
		lab var ta_everjailed "Ever jailed after arrest"
	drop ta_k17arrestedonceormore ta_k18onprobationonceormore ta_k19injailonceormore	
		
	***********************************
	* Relationship outcomes *
	***********************************
	
	*EVER MARRIED 
	gen ta_married = inlist(ta_d1currmaritalstatus,1,3,4,5)
		replace ta_married = . if mi(ta_d1currmaritalstatus)
		bys uid: egen ta_max_married = max(ta_married)
		gen ta_evermarried = (ta_max_married == 1)
			replace ta_evermarried = . if ta_max_married == .
			lab var ta_evermarried "Ever marked as married, divorced, widowed, separated"
	
	*EVER MARRIED AND STILL MARRIED
	gen ta_divorced = inlist(ta_d1currmaritalstatus,4,5)
		replace ta_divorced = . if mi(ta_d1currmaritalstatus)
		bys uid: egen ta_max_divorced = max(ta_divorced)
		gen ta_everdivorced = (ta_max_divorced == 1)
			replace ta_everdivorced = . if ta_max_divorced == .
			lab var ta_everdivorced "Ever marked as divorced or separated"
			
	gen ta_evermarried_neverdiv = (ta_evermarried == 1 & ta_everdivorced == 0)
		replace ta_evermarried_neverdiv = . if mi(ta_evermarried) | mi(ta_everdivorced)
		lab var ta_evermarried_neverdiv "Ever marked as married, never as divorced or separated"

	*EVER COHABITED OR MARRIED
	gen ta_married_or_cohabit = (ta_married ==1 | ta_d4cohabiting ==1)
		replace ta_married_or_cohabit = . if ta_married != 1 & mi(ta_d4cohabiting)
		bys uid: egen ta_max_married_cohabit = max(ta_married_or_cohabit)
		gen ta_evermarriedcohabit = (ta_max_married_cohabit == 1)
			replace ta_evermarriedcohabit = . if ta_max_married_cohabit == .
			lab var ta_evermarriedcohabit "Ever married or cohabiting as if married"
		
	drop ta_married ta_max_married ta_divorced ta_max_divorced ta_married_or_cohabit ta_max_married_cohabit	
	
	*ROMANTIC RELATIONSHIP
	gen ta_current_rom_relationship = (ta_d8romanticrelationshipnow == 1)
		replace ta_current_rom_relationship = 1 if ta_d4cohabiting == 1 | ta_d1currmaritalstatus == 1
		replace ta_current_rom_relationship = . if mi(ta_d8romanticrelationshipnow) & mi(ta_d1currmaritalstatus) & mi(ta_d4cohabiting)
		lab var ta_current_rom_relationship "Currently married, cohabiting, or otherwise in romantic relationship"
		
	*SATISFIED WITH ROMANTIC RELATIONSHIP 
	gen ta_satis_romantic = (ta_d11howsatisfiedw_relationship == 7)
		replace ta_satis_romantic = (ta_c13howsatisfied == 1) if mi(ta_satis_romantic)
		replace ta_satis_romantic = . if mi(ta_c13howsatisfied) & mi(ta_d11howsatisfiedw_relationship)
		lab var ta_satis_romantic "Completely satisfied with current romantic relationship"
		
	drop ta_d11howsatisfiedw_relationship ta_c13howsatisfied 
	
	***********************************
	* Children and pregnancy outcomes *
	***********************************
	
	*MAX NUMBER OF CHILDREN
	bys uid: egen max_num_child = max(ta_d28anumberofchildren)
		lab var max_num_child "Highest listed num of biol, adopted, step-children"
	sort uid year 
	bys uid: gen last_num_child = ta_d28anumberofchildren[_N]
		lab var last_num_child "Last listed number of children in TAS"
		
	drop ta_d28anumberofchildren
	
	*AGE AT FIRST CHILD
	bys uid: egen agefirstchild = min(ta_d28bageat1stchild)
		lab var agefirstchild "Earliest age listed as age of first child"
	bys uid: gen hadchildby20 = (agefirstchild <= 20)
		replace hadchildby20 = . if mi(agefirstchild)
		lab var hadchildby20 "Had first child by age 20"
	drop ta_d28bageat1stchild 
	
	drop ta_d1currmaritalstatus ta_d4cohabiting ta_d8romanticrelationshipnow
	
	***********************************
	* Earnings outcome *
	***********************************

	*CALCULATING 2017 & 2019 INCOME
	replace ta_f1hm = ta_e2aamtofwages_sal + ta_e3aamountofbonusincome + ta_e3bamountofotincome + ta_e3camountoftipsincome + ta_e3damountofcommissionsincome  +ta_e3eamountofotherlaborincome if year == 2017 | year == 2019 
	drop ta_e2aamtofwages_sal ta_e3*amount*
	
	gen pryr = year - 1
	merge m:1 pryr using `cpi', assert(3) nogen

	sum cpi if pryr==2016
	local cpi2016 = `r(mean)'
	
	foreach var of varlist ta_earningsfromworklastyear ta_f1hm {
	gen `var'2016d = `var'/(cpi/100)
	replace `var'2016d = `var'2016d*(`cpi2016'/100)
	drop `var'
	}
	drop cpi pryr
	
	***********************************
	* Unemployment outcomes *
	***********************************
	
	gen ta_workedmoney_pryr = (inlist(ta_e1employmentstatus1stmen,1,2))
		replace ta_workedmoney_pryr = 1 if ta_e3workingformoneynow == 1
		replace ta_workedmoney_pryr = 1 if ta_e3aworkdsincejan1ofprioryear == 1
		replace ta_workedmoney_pryr = . if mi(ta_e1employmentstatus1stmen) & mi(ta_e3workingformoneynow) & mi(ta_e3aworkdsincejan1ofprioryear)
		lab var ta_workedmoney_pryr "Worked for money in the last year"
		label values ta_workedmoney_pryr no_val
	drop ta_e3workingformoneynow ta_e3aworkdsincejan1ofprioryear ta_e1employmentstatus1stmen
	
	*unemployment   
	rename ta_e8aunempandlookedforworklasty ta_unemployed_pryr
	
	**out of labor force
	rename ta_e7aoutoflaborforcelastyear ta_outoflforce_pryr
	
	
********************************************************************************
// SAVE THE DATA

foreach var of varlist _all {
	if substr("`var'",1,3) != "ta_" {
		local newname = "ta_`var'"
		rename `var' `newname'
	}
}
rename ta_ER30001 ER30001 
rename ta_ER30002 ER30002
rename ta_uid uid
rename ta_year year

sort ER30001 ER30002 year
order uid year ER30001 ER30002 
save "`CLEAN'/cleaned_tas.dta", replace









